package com.nannybear.Controller;


import android.content.Context;
import android.os.Handler;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nannybear.Model.EventsCalendar;
import com.nannybear.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

/**
 * \brief Controller of the calendar Screen
 * \author Audrey LOISEAU
 * \date 5-05-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class CalendarController{
    /********** DECLARATIONS **********/
    // Class
    private ViewPuppeteer viewPuppeteer;

    // Variables
    private Calendar calendar;
    private int month, year, actualMonth;
    private static final String dateTemplate = "MMMM yyyy";
    private int currentDayOfMonth;
    private GridCellAdapter gridCellAdapter;
    private List<String> list;
    private final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private String eventToSave;
    private EventsCalendar eventsCalendar;

    // Handler
    private final Handler myHandler = new Handler();

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param viewPuppeteer The ViewPuppeteer
     */
    public CalendarController(ViewPuppeteer viewPuppeteer){
        this.viewPuppeteer = viewPuppeteer;
        this.eventsCalendar = new EventsCalendar();
    }

    /**
     *  \brief Initialization of the interface (creation of components + listeners)
     */
    public void initInterface(){
        list = new ArrayList<>();
        calendar = Calendar.getInstance(Locale.getDefault());
        currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH) +1;
        actualMonth = month;
        year = calendar.get(Calendar.YEAR);

        this.viewPuppeteer.getmActivity().getCalendarScreen().setCurrentMonth((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.currentMonth));
        final ImageButton prevMonth = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.prevMonth);
        final ImageButton nextMonth = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.nextMonth);
        final Button popUp_add = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.pop_up_add);
        final Button popUp_cancel = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.pop_up_cancel);
        final EditText edit_event = (EditText) this.viewPuppeteer.getmActivity().findViewById(R.id.edit_event);


        final ImageButton hour_up_start = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.hour_up_start);
        final ImageButton hour_down_start = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.hour_down_start);
        final ImageButton part_up_start = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.part_up_start);
        final ImageButton hour_up_end = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.hour_up_end);
        final ImageButton hour_down_end = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.hour_down_end);
        final ImageButton part_up_end = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.part_up_end);
        final ImageButton color_btn = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.color_btn);
        final ImageButton min_up_start = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.min_up_start);
        final ImageButton min_down_start = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.min_down_start);
        final ImageButton min_up_end = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.min_up_end);
        final ImageButton min_down_end = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.min_down_end);

        this.viewPuppeteer.getmActivity().getCalendarScreen().setCalendarView((GridView) this.viewPuppeteer.getmActivity().findViewById(R.id.calendar));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setPop_up_calendar((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.pop_up_calendar));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setCalendar_grid((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.calendar_grid));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setDay_pop_up((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.title_pop_up));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setAdd_layout((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.add_layout));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setDelete_layout((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.delete_layout));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setEvent_value((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.event_value));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setListItem((LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.list_layout));

        this.viewPuppeteer.getmActivity().getCalendarScreen().setTxt_hour_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_hour_start));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setTxt_part_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_part_start));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setTxt_hour_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_hour_end));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setTxt_part_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_part_end));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setTxt_min_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_min_start));
        this.viewPuppeteer.getmActivity().getCalendarScreen().setTxt_min_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_min_end));

        saveMonth(month, year);

        gridCellAdapter = new GridCellAdapter(this, (ArrayList)list);
        gridCellAdapter.notifyDataSetChanged();
        this.viewPuppeteer.getmActivity().getCalendarScreen().getCalendarView().setAdapter(gridCellAdapter);

        // Listener
        prevMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = new ArrayList<String>();
                if (month <= 1){
                    month = 12;
                    year--;
                }
                else
                    month--;
                setGridCellAdapterToDate(month, year);
            }
        });
        nextMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = new ArrayList<String>();
                if (month > 11){
                    month = 1;
                    year++;
                }
                else
                    month++;
                setGridCellAdapterToDate(month, year);
            }
        });
        popUp_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getDelete_layout().getVisibility() == View.VISIBLE){
                    viewPuppeteer.getmActivity().getCalendarScreen().displayPopUp(gridCellAdapter.getDateOfEvent());
                }
                else {
                    viewPuppeteer.getmActivity().getCalendarScreen().displayPopUp(null);
                }
            }
        });
        popUp_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getListItem().getVisibility() == View.VISIBLE){
                    viewPuppeteer.getmActivity().getCalendarScreen().displayPopUpAdd();
                }
                else if(viewPuppeteer.getmActivity().getCalendarScreen().getAdd_layout().getVisibility() == View.VISIBLE){
                    checkAndSave();
                }
                else if(viewPuppeteer.getmActivity().getCalendarScreen().getDelete_layout().getVisibility() == View.VISIBLE){
                    viewPuppeteer.getmActivity().getCalendarScreen().displayPopUp(null);
                    eventsCalendar.removeEvents(gridCellAdapter.getDateOfEvent() ,gridCellAdapter.getPosition());
                    gridCellAdapter.notifyDataSetChanged();
                    viewPuppeteer.getmActivity().getCalendarScreen().getCalendarView().setAdapter(gridCellAdapter);
                }
            }
        });
        edit_event.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    if (edit_event.getText().toString().trim().length() > 0) {
                        eventToSave = edit_event.getText().toString();
                    }
                    // close the keyboard
                    InputMethodManager imm = (InputMethodManager) getViewPuppeteer().getmActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(edit_event.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });
        color_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getCalendarScreen().changeIconEvent();
            }
        });
        hour_up_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString().matches("[0-9]+")){
                    int hour = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString());
                    if (hour == 12) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().setText("1");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().setText(String.valueOf(hour + 1));
                    }
                }
            }
        });
        min_up_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().getText().toString().matches("[0-9]+")) {
                    int min = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().getText().toString());
                    if (min == 55) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().setText("0");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().setText(String.valueOf(min + 5));
                    }
                }
            }
        });
        hour_down_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString().matches("[0-9]+")){
                    int hour = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString());
                    if (hour == 1) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().setText("12");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().setText(String.valueOf(hour - 1));
                    }
                }
            }
        });
        min_down_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().getText().toString().matches("[0-9]+")) {
                    int min = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().getText().toString());
                    if (min == 0) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().setText("55");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().setText(String.valueOf(min - 5));
                    }
                }
            }
        });
        part_up_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_start().getText() == "a.m") {
                    viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_start().setText("p.m");
                } else {
                    viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_start().setText("a.m");
                }
            }
        });
        part_up_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_end().getText() == "a.m") {
                    viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_end().setText("p.m");
                } else {
                    viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_end().setText("a.m");
                }
            }
        });
        hour_up_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString().matches("[0-9]+")){
                    int hour = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString());
                    if (hour == 12) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().setText("1");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().setText(String.valueOf(hour + 1));
                    }
                }
            }
        });
        hour_down_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString().matches("[0-9]+")){
                    int hour = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString());
                    if (hour == 1) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().setText("12");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().setText(String.valueOf(hour - 1));
                    }
                }
            }
        });
        min_up_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().getText().toString().matches("[0-9]+")) {
                    int min = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().getText().toString());
                    if (min == 55) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().setText("0");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().setText(String.valueOf(min + 5));
                    }
                }
            }
        });
        min_down_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().getText().toString().matches("[0-9]+")) {
                    int min = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().getText().toString());
                    if (min == 0) {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().setText("55");
                    } else {
                        viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().setText(String.valueOf(min - 5));
                    }
                }
            }
        });

        // Instantiation
        this.viewPuppeteer.getmActivity().getCalendarScreen().setPrevMonth(prevMonth);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setNextMonth(nextMonth);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setPopUp_cancel(popUp_cancel);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setPopUp_add(popUp_add);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setEdit_event(edit_event);

        this.viewPuppeteer.getmActivity().getCalendarScreen().setHour_up_start(hour_up_start);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setHour_down_start(hour_down_start);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setPart_up_start(part_up_start);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setHour_up_end(hour_up_end);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setHour_down_end(hour_down_end);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setPart_up_end(part_up_end);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setColor_btn(color_btn);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setMin_up_start(min_up_start);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setMin_down_start(min_down_start);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setMin_up_end(min_up_end);
        this.viewPuppeteer.getmActivity().getCalendarScreen().setMin_down_end(min_down_end);

        this.viewPuppeteer.getmActivity().getCalendarScreen().getCurrentMonth().setText(DateFormat.format(dateTemplate, calendar.getTime()));
    }

    /**
     * brief Check if the time is good and save the event
     */
    public void checkAndSave(){
        if(viewPuppeteer.getmActivity().getCalendarScreen().getEdit_event().getText().length() > 0) {
            int partDayStart = 12;
            int partDayEnd = 12;
            int hourStart = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString());
            int hourEnd = Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString());
            long timeStart, timeEnd;

            if (viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_start().getText() == "a.m") {
                if (viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString().equals("12")) {
                    hourStart = 0;
                }
                partDayStart = 0;
            }
            if (viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_end().getText() == "a.m") {
                if (viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString().equals("12")) {
                    hourEnd = 0;
                }
                partDayEnd = 0;
            }
            timeStart = (hourStart * 60) + Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().getText().toString()) + partDayStart * 60;
            timeEnd = (hourEnd * 60) + Integer.parseInt(viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().getText().toString()) + partDayEnd * 60;

            if (timeStart > timeEnd) {
                Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "The end time must be greater than the start time", Toast.LENGTH_LONG).show();
            } else if (timeStart <= timeEnd) {
                String timeStartToSave = viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_start().getText().toString() + "." + viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_start().getText().toString() + " " + viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_start().getText();
                String timeEndToSave = viewPuppeteer.getmActivity().getCalendarScreen().getTxt_hour_end().getText().toString() + "." + viewPuppeteer.getmActivity().getCalendarScreen().getTxt_min_end().getText().toString() + " " + viewPuppeteer.getmActivity().getCalendarScreen().getTxt_part_end().getText();
                String timeEvent = timeStartToSave + " - " + timeEndToSave;

                this.eventsCalendar.addEvent(gridCellAdapter.getDateOfEvent(), viewPuppeteer.getmActivity().getCalendarScreen().getIcon(), eventToSave, timeEvent);
                viewPuppeteer.getmActivity().getCalendarScreen().displayPopUp(null);

                gridCellAdapter.notifyDataSetChanged();
                this.viewPuppeteer.getmActivity().getCalendarScreen().getCalendarView().setAdapter(gridCellAdapter);
            }
        }
        else{
            Toast.makeText(viewPuppeteer.getmActivity().getApplicationContext(), "You have to write an event", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * \brief Change the calendar if the user want to change the month
     * \param month The month to display
     * \param year  The year to display
     */
    public void setGridCellAdapterToDate(int month, int year){
        saveMonth(month, year);

        gridCellAdapter = new GridCellAdapter(this, (ArrayList)list);
        calendar.set(year, month - 1, calendar.get(Calendar.DAY_OF_MONTH));
        this.viewPuppeteer.getmActivity().getCalendarScreen().getCurrentMonth().setText(DateFormat.format(dateTemplate, calendar.getTime()));
        gridCellAdapter.notifyDataSetChanged();
        this.viewPuppeteer.getmActivity().getCalendarScreen().getCalendarView().setAdapter(gridCellAdapter);
    }

    /**
     * \brief Give the good visual to the calendar
     * \param month The new month
     * \param year  The new year
     */
    public void saveMonth(int month, int year){
        int space = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;
        int daysInMonth;

        int currentMonth = month - 1;
        daysInMonth = daysOfMonth[currentMonth];

        GregorianCalendar gregCalendar = new GregorianCalendar(year, currentMonth, 1);

        if (currentMonth == 11){
            prevMonth = currentMonth - 1;
            daysInPrevMonth = daysOfMonth[prevMonth];
            nextMonth = 0;
            prevYear = year;
            nextYear = year + 1;
        }
        else if (currentMonth == 0){
            prevMonth = 11;
            prevYear = year - 1;
            nextYear = year;
            daysInPrevMonth = daysOfMonth[prevMonth];
            nextMonth = 1;
        }
        else{
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = year;
            prevYear = year;
            daysInPrevMonth = daysOfMonth[prevMonth];
        }

        space = gregCalendar.get(Calendar.DAY_OF_WEEK) - 1;

        if (gregCalendar.isLeapYear(gregCalendar.get(Calendar.YEAR)) && month == 1){
            ++daysInMonth;
        }

        // Last month days
        for (int i = 0; i < space; i++){
            list.add(String.valueOf((daysInPrevMonth - space + 1) + i) + "-GREY" + "-" + months[prevMonth] + "-" + prevYear);
        }

        // Current month Days
        for (int i = 1; i <= daysInMonth; i++){
            if ((i == this.currentDayOfMonth) && (this.actualMonth == month)) {
                list.add(String.valueOf(i) + "-TODAY" + "-" + months[currentMonth] + "-" + year);
            }
            else
                list.add(String.valueOf(i) + "-BLACK" + "-" + months[currentMonth] + "-" + year);
        }

        // Leading Month days
        for (int i = 0; i < list.size() % 7; i++){
            list.add(String.valueOf(i + 1) + "-GREY" + "-" + months[nextMonth] + "-" + nextYear);
        }
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the ViewPuppeteer class
     */
    public ViewPuppeteer getViewPuppeteer() {
        return viewPuppeteer;
    }

    /**
     * \brief Get method for the EventsCalendar class
     */
    public EventsCalendar getEventsCalendar() {
        return eventsCalendar;
    }
}
